import { request_post, request_get, request_put, request_delete } from 'services/common/request';
import pathToRegexp from 'path-to-regexp';
let toPathGroup = pathToRegexp.compile('/api/data_articles/groups/:id');
/**
 * 获取用户列表
 * @param {Object} data
 * @param {String} data.name - 选填 名称
 * @param {String} data.pageStart
 * @param {String} data.pageOffset
 * @param {String} data.pageNum
 */
export function getList(data = {}) {
	return request_get('/user/userRoleInterfaceList', data).then((res) => {
		res.data.forEach((item) => {
			item.roleId = item.roles && item.roles[0] && item.roles[0].id;
		});
		return res;
	});
}

/**
 * 获取用户
 * @param {Object} data
 * @param {String} data.name - 选填 名称
 * @param {String} data.username
 * @param {String} data.password
 */
export function save(data = {}) {
	if (data.id) {
		return request_put('/user/modifyUsernameOrPasswd', data);
	}
	return request_post('/user/register', data);
}

/**
 * 获取详情
 * @param {Object} data
 * @param {String} data.name - 选填 名称
 * @param {String} data.username
 * @param {String} data.password
 */
export function detail(data = {}) {
	return request_get('/user/modifyPasswd', data);
}

/**
 * 获取详情
 * @param {Object} data
 * @param {String} data.name - 选填 名称
 * @param {String} data.username
 * @param {String} data.password
 */
export function del(data = {}) {
	return request_post('/user/delUser', data);
}

/**
 * 登录
 * @param {Object} data
 * @param {String} data.name - 选填 名称
 * @param {String} data.username
 * @param {String} data.password
 */
export function login(data = {}) {
	return request_post('/user/login', data).then((res) => {
		sessionStorage.token = res.data.token;
		return res;
	});
}
/**
 * 退出
 * @param {Object} data
 * @param {String} data.name - 选填 名称
 */
export function logout(data = {}) {
	return request_get('/user/logout').then((res) => {
		sessionStorage.token = '';
		return res;
	});
}
